/**
 * 
 */
package com.designpattern.design.response;

/**
 * 职责链模式
 * 
 * @author zhangyang
 *
 */
public class ReponseTest {
public static void main(String[] args) {
	//新建好一个请求
	PurchaseRequest request = new PurchaseRequest(1,55000f,1);
	//新建4个审批人
	DepartmentApprove approve = new DepartmentApprove("张院长");
	CollegeApprove collegeApprove = new CollegeApprove("王主任");
	ViceApprove viceApprove = new ViceApprove("李副校长");
	MasterApprove masterApprove = new MasterApprove("东校长");
	//将4个审批人的上下级关系设置
	/**
	 * (最大的下一级设置为最小 形成闭环) 这里根据业务区处理，也可以做成单链 
	 * 环状的问题就是逻辑也要全覆盖 不然最大的校长都处理不了业务就会报错
	 * 环状的好处是减少迭代开销 单链处理就必须要一级一级处理 比较麻烦
	 */
	approve.setApprove(collegeApprove);
	collegeApprove.setApprove(viceApprove);
	viceApprove.setApprove(masterApprove);
	masterApprove.setApprove(approve);
	collegeApprove.doRequest(request);
}
}

/**
 * 审批金额请求
 * 
 * @author zhangyang
 *
 */
class PurchaseRequest {
	// 请求类型
	private int type = 0;
	// 请求金额
	private float price = 0.0f;
	private int id = 0;

	// 构造器
	public PurchaseRequest(int type, float price, int id) {
		this.type = type;
		this.price = price;
		this.id = id;
	}

	public int getType() {
		return type;
	}

	public float getPrice() {
		return price;
	}

	public int getId() {
		return id;
	}

}

/**
 * 抽象请求者
 * 
 * @author zhangyang
 *
 */
abstract class PruchaseApprove {
	// 下一个处理者
	private PruchaseApprove approve;
	// 下一个处理者名字
	private String name;

	public PruchaseApprove(String name) {
		super();
		this.name = name;
	}

	public void setApprove(PruchaseApprove approve) {
		this.approve = approve;
	}

	public void setName(String name) {
		this.name = name;
	}

	public PruchaseApprove getApprove() {
		return approve;
	}

	public String getName() {
		return name;
	}

	// 抽象方法-处理逻辑
	abstract void doRequest(PurchaseRequest request);
}

class DepartmentApprove extends PruchaseApprove {

	/**
	 * @param name
	 */
	public DepartmentApprove(String name) {
		super(name);
		// TODO Auto-generated constructor stub
	}

	@Override
	void doRequest(PurchaseRequest request) {
		// TODO Auto-generated method stub
		if (request.getPrice() <= 5000) {
			System.out.println(request.getId() + "已经被处理,处理人是"+this.getName());
		} else {
			this.getApprove().doRequest(request);
		}
	}

}

class CollegeApprove extends PruchaseApprove {

	/**
	 * @param name
	 */
	public CollegeApprove(String name) {
		super(name);
		// TODO Auto-generated constructor stub
	}

	@Override
	void doRequest(PurchaseRequest request) {
		// TODO Auto-generated method stub
		if (5000<request.getPrice() &&  request.getPrice()<= 10000) {
			System.out.println(request.getId() + "已经被处理,处理人是"+this.getName());
		} else {
			this.getApprove().doRequest(request);
		}
	}

} 

class ViceApprove extends PruchaseApprove {

	/**
	 * @param name
	 */
	public ViceApprove(String name) {
		super(name);
		// TODO Auto-generated constructor stub
	}

	@Override
	void doRequest(PurchaseRequest request) {
		// TODO Auto-generated method stub
		if (10000<request.getPrice() &&  request.getPrice()<= 30000) {
			System.out.println(request.getId() + "已经被处理,处理人是"+this.getName());
		} else {
			this.getApprove().doRequest(request);
		}
	}

} 

class MasterApprove extends PruchaseApprove {

	/**
	 * @param name
	 */
	public MasterApprove(String name) {
		super(name);
		// TODO Auto-generated constructor stub
	}

	@Override
	void doRequest(PurchaseRequest request) {
		// TODO Auto-generated method stub
		if (request.getPrice()> 30000) {
			System.out.println(request.getId() + "已经被处理,处理人是"+this.getName());
		} else {
			this.getApprove().doRequest(request);
		}
	}

} 